package org.zero.manager;

import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author Zero (cnzeropro@qq.com)
 * @date 2021/10/26 20:47
 */
@Component
@Slf4j
public class MyCache {
    private final ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();

    public void addCache(String key, Object value) {
        cache.put(key, value);
        log.info("已添加缓存：{} - {}", key, value);
    }

    public Object getCache(String key) {
        if (!cache.containsKey(key)) {
            log.info("未命中，缓存【{}】不存在", key);
            return null;
        }

        Object obj = cache.get(key);

        log.info("命中，通过【{}】获取到缓存：{}", key, obj);

        return obj;

    }

    public void removeCache(String key) {
        cache.remove(key);

        log.info("已删除缓存：{}", key);
    }
}
